package com.voltmao.service.application.web;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.*;
import com.voltmao.persistent.model.*;
import com.voltmao.service.application.intent.IntentUserServiceImpl;
import com.voltmao.service.application.sys.MessageTemplateServiceImpl;
import com.voltmao.webpro.utils.MD5Utils;
import com.voltmao.webpro.utils.ParamsUtil;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.Date;
import java.util.Map;


/**
* 网站入口服务层
* 注册，填写资料（如基本信息和各种执照）
* @author  zya
* 2018/04/10
* */
@Service
public class WebUserServiceImpl {
    @Autowired
    IntentUserServiceImpl intentUserService;
    @Autowired
    WebUserMapper webUserMapper;

    @Autowired
    IntentUserMapper intentUserMapper;

    @Autowired
    ProviderUserMapper providerUserMapper;

    @Autowired
    SysUserInfoMapper sysUserInfoMapper;
    @Autowired
    AdminUserRoleMapsMapper adminUserRoleMapsMapper;

    @Autowired
    MessageTemplateServiceImpl messageTemplateService;
    /**
    * 注册
    * 1.保存用户名，密码到sys_user
    * 2.保存基本信息及【1】外键到  provider_user,根据类型更新动态信息到其他表
    * 3.
    * 4.生成【2】【3】外键到intent_user
    * */
    @Transactional(rollbackFor = Exception.class)
    public boolean register(Map<String,Object> params) throws RuntimeException{
        String[] checkParams = {"sysUserInfo","providerUser","other"};
        ParamsUtil.checkNotNull(params,checkParams);
        JSONObject tmpSysUser = JSONObject.fromObject(params.get("sysUserInfo"));
        SysUserInfo sysUserInfo = (SysUserInfo)PropertiesUtils.mapToObject(tmpSysUser,SysUserInfo.class);

        //步骤1
        sysUserInfo.setPwd(MD5Utils.getPwd(sysUserInfo.getPwd()));
        sysUserInfo.setDataId(StringUtils.uuid36());
        sysUserInfoMapper.insert(sysUserInfo);


        //步骤2
        JSONObject tmpProviderUser = JSONObject.fromObject(params.get("providerUser"));
        ProviderUser providerUser = (ProviderUser)PropertiesUtils.mapToObject(tmpProviderUser,ProviderUser.class);
        providerUser.setDataId(StringUtils.uuid36());
        providerUser.setCreateDate(new Date());
        providerUser.setForSysUserInfo(sysUserInfo.getDataId());
        providerUser.setIsDel(0);
        providerUser.setForDicQdshzt("0");
        providerUser.setForDicQdsdj("1");

        providerUserMapper.insert(providerUser);
        intentUserService.insertOtherInfo(params,providerUser.getForDicQdlx(),providerUser.getDataId());


        //步骤3

        AdminUserRoleMaps adminUserRoleMaps= new AdminUserRoleMaps();
        adminUserRoleMaps.setDataId(StringUtils.uuid36());
        adminUserRoleMaps.setTblAdminRoleId("role_yxqd");
        adminUserRoleMaps.setTblAdminUserId(sysUserInfo.getDataId());
        adminUserRoleMapsMapper.insert(adminUserRoleMaps);
        //步骤4
        IntentUser intentUser = new IntentUser();
        intentUser.setDataId(StringUtils.uuid36());
        intentUser.setForProviderUser(providerUser.getDataId());
        intentUser.setForDicYxyhzt("0");

        intentUser.setForProviderUser(providerUser.getDataId());
        intentUserMapper.insert(intentUser);
        //通知总监分配
        messageTemplateService.registerInfoToZj(providerUser.getDataId(),"1");
        return true;
    }

    /**
     * 意向渠道重置密码
     * @param params
     * @return
     * @throws RuntimeException
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean resetPwdForIntent(Map<String,Object> params) throws RuntimeException{
        String[] checkParams = {"phoneNo","password"};
        ParamsUtil.checkNotNull(params,checkParams);
        //
        sysUserInfoMapper.resetPwd(params);
        return true;
    }
}
